import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/message.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/message_data_entity.dart';
import 'package:lcapp/model/event/event_bus.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/tools/date_util.dart';

class FaxianMessagePage extends StatefulWidget {
  const FaxianMessagePage({Key? key}) : super(key: key);

  @override
  _FaxianMessagePageState createState() => _FaxianMessagePageState();
}

class _FaxianMessagePageState extends State<FaxianMessagePage> {

  List news = [
    {
      'icon':'assets/faxian/xt_tz.png',
      'name':'系统通知',
      'content':'暂无新消息',
      'routeName':'FaxianSystemPage'
    },
    {
      'icon':'assets/faxian/new_fd.png',
      'name':'新朋友',
      'content':'暂无新消息',
      'routeName':'FaxainNewFriendPage'
    },
    {
      'icon':'assets/faxian/fx_hd.png',
      'name':'互动消息',
      'content':'暂无新消息',
      'routeName':'FaxianHudongPage'
    },
  ];

  MessageDataEntity messageDataEntity = MessageDataEntity();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getSystemMsg();
  }
  getSystemMsg()async{
    ResponseData res = await MessageAPI.getLastMessage();
    if(res.code==200&&res.data!=null){
      if(mounted){
        setState(() {
          messageDataEntity = MessageDataEntity().fromJson(res.data);
        });
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.symmetric(horizontal: 30.w),
      child: EasyRefresh(
        header: myRefreshHeader(),
        onRefresh: ()async{
          getSystemMsg();
        },
        child: ListView.builder(
            itemCount: messageDataEntity==null?0: news.length,
            itemBuilder: (context,index){
              MessageItem? messageItem;
              if(index==0){
                messageItem = messageDataEntity.sysMsg;
              }else if(index==1){
                messageItem = messageDataEntity.friendMsg;
              }else{
                messageItem = messageDataEntity.hdMsg;
              }
              return GestureDetector(
                onTap: () async{
                  Navigator.of(context).pushNamed(news[index]['routeName']);
                  if(index==0){
                    messageDataEntity.sysUnReadCount = 0;
                    await MessageAPI.updateMessageState(session: 1);
                  }else if(index==1){
                    messageDataEntity.friendUnReadCount = 0;
                    await MessageAPI.updateMessageState(session: 3);
                  }else if(index==2){
                    messageDataEntity.hdUnReadCount = 0;
                    await MessageAPI.updateMessageState(session: 2);
                  }
                  eventBus.fire(MessageUnReadState());
                  setState((){});
                },
                child: Container(
                  color: Colors.transparent,
                  height: 140.w,
                  margin: EdgeInsets.only(bottom: 40.w),
                  child: Column(
                    children: [
                      Expanded(child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        children: [
                          Image.asset(news[index]['icon'],width: 90.w,),
                          SizedBox(width: 30.w,),
                          Expanded(child: Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Text(news[index]['name'],
                                style: TextStyle(
                                    color: Colors.black,
                                    fontSize: 32.sp,
                                    fontWeight: FontWeight.w600
                                ),),
                              SizedBox(height: 10.w,),
                              Text('${messageItem!=null? messageItem.content??news[index]['content']:news[index]['content']}',maxLines: 1,overflow: TextOverflow.ellipsis,style: TextStyle(
                                color: Color(0xff848484),
                                fontSize: 28.sp,
                              ))
                            ],
                          )),
                          Column(
                            mainAxisAlignment: MainAxisAlignment.center,
                            children: [
                              Visibility(child: Text('${messageItem!=null? DateUtil.getDateScope(checkDate: messageItem.createTime):''}',style: TextStyle(
                                color: Color(0xff848484),
                                fontSize: 26.sp,
                              )),visible: messageItem!=null&&messageItem.createTime!=null,),
                              SizedBox(height: 20.w,),
                              Visibility(child: Container(
                                width: 76.w,
                                height: 40.w,
                                alignment: Alignment.center,
                                decoration: BoxDecoration(
                                    color: Color(0xffFB302A),
                                    borderRadius: BorderRadius.circular(40.w)
                                ),
                                child: Text(index==0?'${messageDataEntity.sysUnReadCount??0}':index==1?'${messageDataEntity.friendUnReadCount??0}':'${messageDataEntity.hdUnReadCount??0}',style: TextStyle(
                                  color: Colors.white,
                                  fontSize: 24.sp,
                                )),
                              ),visible: messageDataEntity!=null&&index==0?messageDataEntity.sysUnReadCount!=0:index==1?messageDataEntity.friendUnReadCount!=0:messageDataEntity.hdUnReadCount!=0,)
                            ],
                          )
                        ],
                      )),
                      Container(
                        height: 1,
                        margin: EdgeInsets.only(left: 120.w),
                        color: Color(0xffE9E8E9),
                      ),
                    ],
                  ),
                ),
              );
            },
        ),
      ),
    );
  }
}
